package com.qr.activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.qr.activitys.bean.State;
import com.qr.activitys.http.ApiService;
import com.qr.activitys.http.NetWorkMgr;
import com.qr.activitys.http.api.HomeAPI;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 项目名称：
 * 类描述：首页 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2018/5/9 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2018/5/9 0027 下午 1:33
 * 修改备注：
 */
public class HomeManager {

    private static HomeAPI mHomeAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    private static class HomeApiManagerHolder {
        private static final HomeManager INSTANCE = new HomeManager();
    }
    public static final HomeManager getHomeApiManager(Activity activity, int taskId) {
        mHomeAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(HomeAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return HomeApiManagerHolder.INSTANCE;
    }

    /**
     *  排队机数据 发送
     */
    public void httpPostData(String json) {
        Subscription subscription = mHomeAPI
                .httpPostData(json).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<State>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //log 打印
                        Log.e("http:-------------->","structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(State state) {

                        //log 打印
                        Log.e("http:-------------->",state.getState());
                        Log.e("http:-------------->","数据已发送成功！");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }
}
